<template>
	<div>
		<div class="pb10px">
			<el-form ref="searchForm" :model="searchForm" inline>
				<el-form-item label="考试标题" prop="name">
					<el-input v-model="searchForm.name" clearable placeholder="请输入考试标题"></el-input>
				</el-form-item>
				<el-form-item label="">
					<el-button type="primary" icon="el-icon-search" @click="handleSearch">搜 索</el-button>
					<el-button icon="el-icon-refresh-left" @click="resetForm">重 置</el-button>
				</el-form-item>
			</el-form>
		</div>
		<el-table ref="multipleTable" :data="listData" tooltip-effect="dark" style="width: 100%"
			:header-cell-style="headClass" :cell-style="rowClass">
			<el-table-column label="序号" type="index" width="55"></el-table-column>
			<el-table-column prop="name" label="考试标题"></el-table-column>
			<el-table-column label="封面图">index
				<template slot-scope="scope">
					<el-image style="width: 60px; height: 60px" :src="scope.row.img"
						:preview-src-list="[scope.row.img]"></el-image>
				</template>
			</el-table-column>
			<el-table-column prop="status" label="发布状态">
				<template slot-scope="scope">
					<div v-if="scope.row.status == 1">
						<div class="greenBox">
							已发布
						</div>
					</div>
					<div v-else-if="scope.row.status != 1">
						<div class="guleBox">
							未发布
						</div>
					</div>
				</template>
			</el-table-column>
			<el-table-column prop="type" label="考试状态">
				<template slot-scope="scope">
					<div v-if="scope.row.type == '未开始'">
						<div class="guleBox">
							未开始
						</div>
					</div>
					<div v-else-if="scope.row.type == '进行中'">
						<div class="greenBox">
							进行中
						</div>
					</div>
					<div v-else-if="scope.row.type == '已结束'">
						<div class="blueBox">
							已结束
						</div>
					</div>
				</template>
			</el-table-column>
			<el-table-column prop="startTime" label="开始时间" width="200"></el-table-column>
			<el-table-column prop="endTime" label="结束时间" width="200"></el-table-column>
			<el-table-column prop="user" label="发布人"></el-table-column>
			<el-table-column label="操作" width="400" fixed="right">
				<template slot-scope="scope">
					<el-button type="success" icon="el-icon-view" @click="look(scope.row.id,scope.row.name)">
						查看成绩
					</el-button>
					<el-button icon="el-icon-edit" @click="goScore(scope.row.id,scope.row.name,scope.row.type)">
						去评分
					</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div style="margin-top: 20px">
			<el-pagination @current-change="currentChange" @size-change="sizeChange" :page-size="pageInfo.pageSize"
				:current-page="pageInfo.currentPage" background layout="sizes, prev, pager, next" :total="total">
			</el-pagination>
		</div>
		<el-dialog :title="formData.id?'编辑':'新增'" :visible.sync="isDrawerDialog" size="500px" direction="rtl"
			:wrapper-closable="false">
			<div class="demo-drawer_content" style="padding-right: 40px;box-sizing: border-box">
				<el-form :model="formData" label-width="100px" class="demo-ruleForm" :rules="rules" ref="formData">
					<slot name="content">
						<el-form-item label="考试标题" prop="name">
							<el-input v-model="formData.name"></el-input>
						</el-form-item>
						<el-form-item label="封面图" prop="img">
							<el-upload style="text-align: center;padding-bottom: 10px;" :action="$httpUploadFile"
								:show-file-list="false" :on-success="uploadImgSuccess">
								<img v-if="formData.img" :src="formData.img"
									style="width: 150px;height: 150px;display: block;">
								<i v-else class="el-icon-plus"
									style="font-size: 28px;color: #8c939d;width: 150px;height: 150px;line-height: 150px;text-align: center;border: 1px solid #ccc;"></i>
							</el-upload>
						</el-form-item>
						<el-form-item label="开始时间" prop="startTime">
							<el-date-picker v-model="formData.startTime" type="datetime"
								value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
						</el-form-item>
						<el-form-item label="结束时间" prop="endTime">
							<el-date-picker v-model="formData.endTime" type="datetime"
								value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
						</el-form-item>
						<el-form-item label="试卷列表" prop="examPaper">
							<el-select v-model="formData.examPaper" collapse-tags style="width:100%" placeholder="请选择">
								<el-option v-for="item in tableData" :key="item.id" :label="item.name" :value="item.id">
								</el-option>
							</el-select>
						</el-form-item>
						<el-form-item label="绑定报名" prop="applicationId">
							<el-select v-model="formData.applicationId" collapse-tags style="width:100%"
								placeholder="请选择">
								<el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
								</el-option>
							</el-select>
						</el-form-item>
						<el-form-item label="是否发布" prop="status">
							<el-radio v-model="formData.status" label="1">发布</el-radio>
							<el-radio v-model="formData.status" label="0">不发布</el-radio>
						</el-form-item>
						<el-form-item label="题目乱序" prop="sequence">
							<el-radio v-model="formData.sequence" label="1">乱序</el-radio>
							<el-radio v-model="formData.sequence" label="0">不乱序</el-radio>
						</el-form-item>
						<!-- <el-form-item label="考试总分">
							<el-input v-model="formData.score" disabled></el-input>
						</el-form-item> -->
					</slot>
				</el-form>
			</div>
			<div class="demo-drawer_footer" style="display: flex; justify-content: space-around;">
				<el-button @click="isDrawerDialog = false">取消</el-button>
				<el-button type="primary" @click="submit('formData')">提交</el-button>
			</div>
		</el-dialog>
		<el-dialog title="考试详情" :visible.sync="dialogTableVisible">
			<div class="kaoshiBox">
				<div style="width: 35%;">
					<el-image style="width: 100%; min-height: 150px;" :src="examList.img"
						:preview-src-list="[examList.img]">
					</el-image>
				</div>
				<div class="rightBox">
					<div class="timeBox">
						<div style="font-size: 18px;width: 50%;font-weight: bold;">考试名称：{{examList.name}}</div>
						<div style="font-size: 16px;width: 50%;">
							<el-popover placement="top" title="标题" width="400" trigger="click">
								<el-table :data="applicationList">
									<el-table-column label="序号" type="index" width="50"></el-table-column>
									<el-table-column property="username" label="姓名"></el-table-column>
									<el-table-column property="gender" label="性别"></el-table-column>
									<el-table-column property="mobile" label="手机号"></el-table-column>
									<el-table-column property="idCard" label="身份证"></el-table-column>
									<el-table-column property="email" label="邮箱"></el-table-column>
								</el-table>
								<el-button slot="reference" type="success">查看报名</el-button>
							</el-popover>
						</div>
					</div>
					<div class="timeBox">
						<div style="font-size: 16px;width: 50%;">开始时间：{{examList.startTime}}</div>
						<div style="font-size: 16px;width: 50%;">结束时间：{{examList.endTime}}</div>
					</div>
					<div class="timeBox">
						<div style="font-size: 16px;width: 50%;">报名人数：{{application_count}}</div>
						<div style="font-size: 16px;width: 50%;">实际考试人数：{{exam_count}}</div>
					</div>
					<div class="timeBox">
						<div style="font-size: 16px;width: 50%;" v-if="examList.unify == '1'">考试模式：统一开考</div>
						<div style="font-size: 16px;width: 50%;" v-if="examList.unify == '0'">考试模式：时间段内自由考试</div>
						<div style="font-size: 16px;width: 50%;" v-if="examList.sequence == '1'">是否乱序：乱序</div>
						<div style="font-size: 16px;width: 50%;" v-if="examList.sequence == '0'">是否乱序：不乱序</div>
					</div>
				</div>
			</div>
			<div style="margin-top: 50px;">
				<el-table :data="gridData" max-height="300" :header-cell-style="headClass" :cell-style="rowClass">
					<el-table-column label="序号" type="index" width="50">
					</el-table-column>
					<el-table-column property="userName" label="姓名"></el-table-column>
					<el-table-column property="isOk" label="是否交卷">
					</el-table-column>
					<el-table-column property="okTime" label="交卷时间" width="200"></el-table-column>
					<el-table-column property="isScore" label="是否评分"></el-table-column>
					<el-table-column property="score" label="总分">

					</el-table-column>
					<el-table-column label="操作">
						<template slot-scope="scope">
							<el-button size="mini" type="danger" @click="handleDelete1(scope.$index, scope.row)">删除
							</el-button>
						</template>
					</el-table-column>
				</el-table>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import utils from "@/utils/utils";
	import {
		Result
	} from "element-ui";

	export default {
		data() {
			return {
				//抽屉控制
				isDrawerDialog: false,
				dialogTableVisible: false,
				//列表数据
				listData: [],
				chooseId: '',
				tableData: [],
				visible: false,
				gridData: [],
				applicationList: [],
				examList: [],
				rules: {
					name: [{
						required: true,
						message: '请输入考试名称',
						trigger: 'blur'
					}],
					startTime: [{
						required: true,
						message: '请选择开始时间',
						trigger: 'change'
					}],
					endTime: [{
						required: true,
						message: '请选择结束时间',
						trigger: 'change'
					}],
					applicationId: [{
						required: true,
						message: '请选择绑定报名',
						trigger: 'change'
					}],
					examPaper: [{
						required: true,
						message: '请选择考试试卷',
						trigger: 'change'
					}],
					status: [{
						required: true,
						message: '请选择发布状态',
						trigger: 'change'
					}],
					sequence: [{
						required: true,
						message: '请选择是否乱序',
						trigger: 'change'
					}],
					unify: [{
						required: true,
						message: '请选择开考模式',
						trigger: 'change'
					}],
				},
				//总条数
				total: 0,
				application_count: 0,
				exam_count: 0,
				//分页详情
				pageInfo: {
					//当前页
					pageNum: 1,
					//分页大小
					pageSize: 10
				},
				//搜索
				searchForm: {
					name: ""
				},
				userList: [],
				flag: false,
				formData: {
					status: '',
					sequence: ''
				},
				//删除数据的id
				ids: [],
				userInfo: utils.getUserInfo(),
				options: [],

			}
		},

		created() {
			this.getPageList()
			this.getExamList()
			this.getApplicationList()
		},
		methods: {
			//获取分页数据
			getPageList() {
				let data = Object.assign(this.searchForm, this.pageInfo)
				this.$axios.get("/exam/page", {
					params: data
				}).then(res => {
					res.data.list.forEach((item) => {

						let nowTime = parseInt(new Date().getTime())
						let startTime = parseInt(new Date(item.startTime).getTime())
						let endTime = parseInt(new Date(item.endTime).getTime())
						if (startTime > nowTime) {
							item.type = '未开始'
						} else if (startTime < nowTime && nowTime < endTime) {
							item.type = '进行中'

						} else if (nowTime > endTime) {
							item.type = '已结束'
						}
					})
					this.listData = res.data.list
					this.total = res.data.total
					console.log(55, this.listData)
				})
			},
			//获取报名列表
			getApplicationList() {
				let searchForm = {
					name: "",
					status: ''
				}
				let pageInfo = {
					pageNum: 1,
					//分页大小
					pageSize: 10000
				}
				let data = Object.assign(searchForm, pageInfo)
				this.$axios.get("/application/page", {
					params: data
				}).then(res => {
					console.log(123, res)
					this.options = res.data.data
				})
			},
			look(id) {
				if (id) {
					this.chooseId = id
				}
				this.$axios.get("/exam/getExamInfoById?id=" + this.chooseId).then(res => {
					console.log(res)
					this.gridData = res.data.exam_info
					this.application_count = res.data.application_count
					this.exam_count = res.data.exam_count
					this.applicationList = res.data.application_info
				})
				this.$axios.get("/exam/getById?id=" + id).then(result => {
					this.examList = result.data
					this.dialogTableVisible = true
					console.log('考试', this.examList)
				})
			},
			goScore(id, name, type) {
				console.log(type)
				this.$axios.get("/exam/getExamInfoById?id=" + id).then(res => {
					console.log(res)
					// 	this.gridData = res.data.exam_info
					// 	this.application_count = res.data.application_count
					// 	this.exam_count = res.data.exam_count
					this.userList = res.data.exam_info
					this.userList.forEach(item => {
						if (item.isOk == '否') {
							this.flag = true
							return
						}
					})

					if (type == '已结束' && this.flag == false) {
						this.$router.push({
							path: '/scorePoint',
							query: {
								id: id,
								name: name
							}
						})
					} else if (type == '进行中' && this.flag == false) {
						this.$message.error('考试未结束')
					}else if(type == '已结束' && this.flag == true){
						this.$message('还有考生未交卷')
					}else{
						console.log(this.flag,type)
						this.$message('请确认考试是否结束，是否有考生未交卷')
					}
				})
			},
			getExamList() {
				let searchForm = {
					name: "",
				}
				let pageInfo = {
					pageNum: 1,
					//分页大小
					pageSize: 10000
				}
				let data = Object.assign(searchForm, pageInfo)
				this.$axios.get("/examPaper/page", {
					params: data
				}).then(res => {
					console.log(123, res)
					this.tableData = res.data.data
					this.tableData.forEach(item => {
						if (item.questionId != null) {
							item.count = item.questionId.split(',')
							item.count = item.count.length
						} else {
							item.count = 0
						}

						// item.count = item.count.length
					})
				})
			},
			resetForm() {
				this.$refs['searchForm'].resetFields();
			},
			cleanForm() {
				this.$refs['formData'].resetFields();
			},
			// getScore(){

			// 	this.tableData.forEach(item=>{
			// 		if(item.id == this.formData.examPaper){
			// 			this.formData.score = item.score
			// 		}
			// 	})
			// },
			//选择分页
			currentChange(e) {
				this.pageInfo.pageNum = e
				this.getPageList()
			},
			//改变分页数量
			sizeChange(e) {
				this.pageInfo.pageSize = e
				this.getPageList()
			},
			//搜索
			handleSearch() {
				this.pageInfo.pageNum = 1
				this.getPageList()
			},
			headClass() { //表头居中显示
				return "text-align:center;font-size:14px;"
			},
			rowClass() { //表格数据居中显示
				return "text-align:center;font-size:13px;"
			},
			//编辑
			handleEdit(index, row) {
				this.formData = Object.assign({}, row)
				console.log(this.formData)
				this.isDrawerDialog = true
			},
			//单个删除
			handleDelete1(index, row) {
				this.delScore([row])
			},
			delScore(rows) {
				if (!rows) {
					rows = this.$refs.multipleTable.selection;
				}
				let ids = rows.map(item => item.id);
				this.$confirm(`此操作将永久删除ID为[${ids}]的数据, 是否继续?`, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning',
					center: true
				}).then(() => {
					//批量删除数据
					this.$axios.delete("/score/delBatch", {
						data: ids
					}).then(res => {
						if (res.code === 200) {
							this.$message({
								message: res.msg,
								type: 'success'
							});
							this.look()
						}
					})
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
					this.$refs.multipleTable.clearSelection();
				});
			},
			//单个删除
			handleDelete(index, row) {
				this.batchDelete([row])
			},
			//批量删除
			batchDelete(rows) {
				if (!rows) {
					rows = this.$refs.multipleTable.selection;
				}
				let ids = rows.map(item => item.id);
				this.$confirm(`此操作将永久删除ID为[${ids}]的数据, 是否继续?`, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning',
					center: true
				}).then(() => {
					//批量删除数据
					this.$axios.delete("/exam/delBatch", {
						data: ids
					}).then(res => {
						if (res.code === 200) {
							this.$message({
								message: res.msg,
								type: 'success'
							});
							this.getPageList()
						}
					})
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
					this.$refs.multipleTable.clearSelection();
				});
			},
			uploadImgSuccess(res) {
				this.$set(this.formData, "img", res.data.url)
			}
		}
	};
</script>

<style scoped>
	.greenBox {
		background-color: #67C23A;
		border-radius: 5px;
		color: white;
	}

	.titleBox {
		font-size: 18px;
		font-weight: bold;
	}

	.rightBox {
		font-size: 16px;
		width: 60%;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		padding: 15px 10px;
	}

	.timeBox {
		width: 100%;
		display: flex;
		flex-direction: row;
	}

	.kaoshiBox {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		box-shadow: 0px 0px 5px 2px gainsboro;
		border-radius: 5px;
	}

	.guleBox {
		background-color: #909399;
		border-radius: 5px;
		color: white;
	}

	.blueBox {
		background-color: #409EFF;
		border-radius: 5px;
		color: white;
	}
</style>
